<?php
/**
* @author		Arunoda Susiripala
* @package		jconnect
* @copyright	Arunoda Susiripala
* @license 		http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
*/
if(file_exists("config.php")){
	include_once 'config.php';
}

if(isset($_REQUEST['install'])){
	$file_data="<?php ";
	$file_data.='$db_host="'.$_REQUEST['db_host'].'"; '."\n";
	$file_data.='$db_user="'.$_REQUEST['db_user'].'"; '."\n";
	$file_data.='$db_password="'.$_REQUEST['db_password'].'";'."\n";
	$file_data.='$db_name="'.$_REQUEST['db_name'].'"; '."\n";
	$file_data.='$prefix="'.$_REQUEST['db_prefix'].'"; '."\n";

	if(is_writeable('config.php')){

		$file=fopen('config.php','w');
		fwrite($file,$file_data);
		fclose($file);

		$succes=false;

		$db_conn=@mysql_connect($_REQUEST['db_host'],$_REQUEST['db_user'],$_REQUEST['db_password']);
		$is_db_load=mysql_select_db($_REQUEST['db_name']);
		$db_file=file("db.sql");
		$sql=implode("",$db_file);

		$sql_arr=explode(";",$sql);
		$is_query_load;
		foreach ($sql_arr as $query){
			if(!trim($query)) continue;
			$is_query_load=mysql_query($query);
		}

		if(!$db_conn){
			echo "<h2>Database information is not correct</h2>";
		}
		else if(!$is_db_load){
			echo "<h2>Database cannot be load</h2>";
		}
		else if(!$is_query_load){
			echo "<h2>".mysql_error()."</h2>";
		}
		else{
			$succes=true;
			echo "<h2>Installation Done!</h2>";
			echo "<a href='setup.php'>Set Up ExApp</a>";
			echo "<p></p>";
			echo "<font color='green'>";
			echo "<h3>Details to JConnect</h3><ul>";
			echo "<li>Host : ".getenv('HTTP_HOST')."</li>";
			echo "<li>Path : ".str_replace("install.php","server.php",getenv('SCRIPT_NAME'))."</li>";
			echo "<li>Port : ".getenv('SERVER_PORT')."</li>";
			echo "</ul><font>";
		}
	}
	else{
		echo "<h2>Config file cannot be saved to the File System</h2>";
		echo "please save following code as `config.php` in the ExApp root...<p>";
		echo "<textarea rows=10 cols=40>$file_data</textarea><p>";
	}
}
?>

<?php if(!$succes) {?>
<html>
<head>
<a href="index.php">Home</a>
<br></br>
<form method='post'>
<table>
	<tr>
		<td>Database Host</td>
		<td><input type="text" name="db_host" value='<?php echo $db_host;?>' /></td>
	</tr>
	<tr>
		<td>Database User</td>
		<td><input type="text" name="db_user" value='<?php echo $db_user;?>' /></td>
	</tr>
	<tr>
		<td>Database Password</td>
		<td><input type="text" name="db_password"
			value='<?php echo $db_password;?>' /></td>
	</tr>
	<tr>
		<td>Database Name</td>
		<td><input type="text" name="db_name" value='<?php echo $db_name;?>' /></td>
	</tr>
	<tr>
		<td>Database Prefix</td>
		<td><input type="text" name="db_prefix" value='<?php echo $prefix;?>' /></td>
	</tr>
	<tr>
		<td colspan='2'><input type='submit' name='install' value="Install" /></td>
	</tr>
</table>
</form>
</head>
</html>

<?php }?>



